<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
</head>

<body>
  <h1>02-forin和forof区别</h1>
  <script>
    Object.prototype.objCustom = function () { }
    Array.prototype.arrCustom = function () { }

    let arr = [3, 5, 7]
    arr.foo = 'hello'

    console.log(arr)

    // 不要用 for in 遍历数组，会遍历所有可枚举的属性，包括原型上的属性
    for (const key in arr) {
      console.log(key)
    }

    console.log('2-------------------------')
    for (let i in arr) {
      // 不遍历原型的属性
      if (arr.hasOwnProperty(i)) {
        console.log(i) // logs 0, 1, 2, "foo"
      }
    }

    console.log('3-------------------------')
    for (let i of arr) {
      console.log(i) // logs 3, 5, 7
    }
  </script>
</body>

</html>